import Vue from 'vue';
import Router from 'vue-router';
Vue.use(Router);
import store from '../store';
import { getString } from '@utils/utils';
import { Toast } from 'vant';

import { user_getUserBasicInfo, login_wxLogin, login_authWxCallBack } from '@api/user';

const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL, //BASE_URL取值于vue.config.js中publicPath
  routes: [
    {
      path: '/',
      redirect: '/service'
    },
    {
      path: '/login',
      name: 'login',
      component: r => require.ensure([], () => r(require('@pages/mine/login.vue')), 'Login'),
      meta: {
        title: '腾云企服客户中心'
      }
    },
    {
      path: '/invitation',
      name: 'invitation',
      component: r => require.ensure([], () => r(require('@pages/mine/invitation.vue')), 'Invitation'),
      meta: {
        title: '腾云企服客户中心'
      }
    },
    {
      path: '/service',
      name: 'service',
      component: r => require.ensure([], () => r(require('@pages/service/index.vue')), 'Service'),
      meta: {
        title: '服务'
      }
    },
    {
      path: '/invoice',
      name: 'invoice',
      component: r => require.ensure([], () => r(require('@pages/service/invoice.vue')), 'invoice'),
      meta: {
        title: '税务筹划'
      }
    },
    {
      path: '/inputInvoiceApply',
      name: 'inputInvoiceApply',
      component: r => require.ensure([], () => r(require('@pages/service/inputInvoiceApply.vue')), 'InputInvoiceApply'),
      meta: {
        title: '申请进项发票'
      }
    },
    {
      path: '/electronicInvoice',
      name: 'electronicInvoice',
      component: r => require.ensure([], () => r(require('@pages/service/electronicInvoice.vue')), 'electronicInvoice'),
      meta: {
        title: '申请电子发票'
      }
    },
    {
      path: '/electronicInvoiceApply',
      name: 'electronicInvoiceApply',
      component: r => require.ensure([], () => r(require('@pages/service/electronicInvoiceApply.vue')), 'electronicInvoice'),
      meta: {
        title: '申请电子发票'
      }
    },
    {
      path: '/parkTaxRebate',
      name: 'parkTaxRebate',
      component: r => require.ensure([], () => r(require('@pages/service/parkTaxRebate.vue')), 'RebateDetail'),
      meta: {
        title: '申请园区返税'
      }
    },
    {
      path: '/lawService',
      name: 'lawService',
      component: r => require.ensure([], () => r(require('@pages/service/lawService.vue')), 'lawService'),
      meta: {
        title: '法律服务'
      }
    },
    {
      path: '/lawServiceApply',
      name: 'lawServiceApply',
      component: r => require.ensure([], () => r(require('@pages/service/lawServiceApply.vue')), 'lawServiceApply'),
      meta: {
        title: '法律服务'
      }
    },
    {
      path: '/loanProductDetail',
      name: 'loanProductDetail',
      component: r => require.ensure([], () => r(require('@pages/service/loanProductDetail.vue')), 'loanProductDetail'),
      meta: {
        title: '贷款产品详情'
      }
    },
    {
      path: '/loanProductApply',
      name: 'loanProductApply',
      component: r => require.ensure([], () => r(require('@pages/service/loanProductApply.vue')), 'loanProductDetail'),
      meta: {
        title: '贷款产品申请'
      }
    },
    {
      path: '/productApply',
      name: 'productApply',
      component: r => require.ensure([], () => r(require('@pages/service/productApply.vue')), 'productApply'),
      meta: {
        title: '产品申请'
      }
    },
    {
      path: '/bussiness',
      name: 'bussiness',
      component: r => require.ensure([], () => r(require('@pages/bussiness/index.vue')), 'Bussiness'),
      meta: {
        title: '商机'
      }
    },
    {
      path: '/bussinessDetail',
      name: 'bussinessDetail',
      component: r => require.ensure([], () => r(require('@pages/bussiness/bussinessDetail.vue')), 'BussinessDetail'),
      meta: {
        title: '商机详情',
        noLogin: true
      }
    },
    {
      path: '/bussinessRelease',
      name: 'bussinessRelease',
      component: r => require.ensure([], () => r(require('@pages/bussiness/bussinessRelease.vue')), 'BussinessRelease'),
      meta: {
        title: '发布商机'
      }
    },
    {
      path: '/mine',
      name: 'mine',
      component: r => require.ensure([], () => r(require('@pages/mine/index.vue')), 'Mine'),
      meta: {
        title: '我的'
      }
    },
    {
      path: '/guide',
      name: 'guide',
      component: r => require.ensure([], () => r(require('@pages/service/guide.vue')), 'Guide'),
      meta: {
        title: '关注公众号',
        noLogin: true
      }
    },
    {
      path: '/consultant',
      name: 'consultant',
      component: r => require.ensure([], () => r(require('@pages/mine/consultant.vue')), 'Consultant'),
      meta: {
        title: '专属顾问'
      }
    },
    {
      path: '/collectBussiness',
      name: 'collectBussiness',
      component: r => require.ensure([], () => r(require('@pages/mine/collectBussiness.vue')), 'CollectBussiness'),
      meta: {
        title: '我的商机'
      }
    },
    {
      path: '/invoiceList',
      name: 'invoiceList',
      component: r => require.ensure([], () => r(require('@pages/mine/invoiceList.vue')), 'InvoiceList'),
      meta: {
        title: '发票抬头'
      }
    },
    {
      path: '/invoiceDetail',
      name: 'invoiceDetail',
      component: r => require.ensure([], () => r(require('@pages/mine/invoiceDetail.vue')), 'InvoiceDetail'),
      meta: {
        title: '发票详情'
      }
    },
    {
      path: '/invoiceManage',
      name: 'invoiceManage',
      component: r => require.ensure([], () => r(require('@pages/mine/invoiceManage.vue')), 'InvoiceManage'),
      meta: {
        title: '发票抬头'
      }
    },
    {
      path: '/poster',
      name: 'poster',
      component: r => require.ensure([], () => r(require('@pages/mine/poster.vue')), 'Poster'),
      meta: {
        title: '我的海报'
      }
    },
    {
      path: '/userInfo',
      name: 'userInfo',
      component: r => require.ensure([], () => r(require('@pages/mine/userInfo.vue')), 'UserInfo'),
      meta: {
        title: '个人信息'
      }
    },
    {
      path: '/modifyPhone',
      name: 'modifyPhone',
      component: r => require.ensure([], () => r(require('@pages/mine/modifyPhone.vue')), 'ModifyPhone'),
      meta: {
        title: '修改手机号'
      }
    },
    {
      path: '/gift',
      name: 'gift',
      component: r => require.ensure([], () => r(require('@pages/gift/index.vue')), 'Gift'),
      meta: {
        title: '礼包'
      }
    },
    {
      path: '/confirmOrder',
      name: 'confirmOrder',
      component: r => require.ensure([], () => r(require('@pages/gift/confirmOrder.vue')), 'ConfirmOrder'),
      meta: {
        title: '确认订单'
      }
    },
    {
      path: '/myOrder',
      name: 'myOrder',
      component: r => require.ensure([], () => r(require('@pages/mine/myOrder.vue')), 'MyOrder'),
      meta: {
        title: '我的订单'
      }
    },
    {
      path: '/apply',
      name: 'apply',
      component: r => require.ensure([], () => r(require('@pages/service/apply.vue')), 'Apply'),
      meta: {
        title: '微业贷申请',
        noLogin: true
      }
    },
    {
      path: '/cardList',
      name: 'cardList',
      component: r => require.ensure([], () => r(require('@pages/mine/cardList.vue')), 'CardList'),
      meta: {
        title: '名片列表'
      }
    },
    {
      path: '/zfbSuccess',
      name: 'zfbSuccess',
      component: r => require.ensure([], () => r(require('@pages/gift/zfbSuccess.vue')), 'ZfbSuccess'),
      meta: {
        title: '支付成功',
        noLogin: true
      }
    }
  ]
});

router.beforeEach(async (to, from, next) => {
  const {
    openId,
    token,
    userInfo: { phone }
  } = store.state;
  if (to.meta.noLogin || phone) {
    next();
  } else {
    if (token) {
      let res = await user_getUserBasicInfo();
      if (res.code === 200) {
        // localStorage.removeItem('openId');
        localStorage.setItem('openId', res.data.openId);
        store.commit('setUserInfo', res.data);
        next();
      } else {
        Toast(res.msg);
      }
    } else if (!openId) {
      let code = getString('code');
      if (!code) {
        let res = await login_wxLogin({ redirectUrl: location.href });
        if (res.code === 200) {
          location.href = res.data;
        } else {
          Toast(res.msg);
        }
      } else {
        let res = await login_authWxCallBack({ code });
        if (res.code === 200) {
          localStorage.setItem('openId', res.data);
          store.commit('setOpenid', res.data);
          next();
        } else {
          Toast(res.msg);
        }
      }
    } else {
      next();
    }
  }
});
router.afterEach(to => {
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  Vue.prototype.wxJssdk.hideMenu();
  document.getElementById('app').scrollTop = 0;
});

Router.prototype.openPage = function(link, query) {
  this.push({
    path: link,
    query: Object.assign(
      {
        time: new Date().getTime()
      },
      query || {}
    )
  });
};
Router.prototype.replacePage = function(link, query) {
  this.replace({
    path: link,
    query: Object.assign(
      {
        time: new Date().getTime()
      },
      query || {}
    )
  });
};

export default router;
